package com.project.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final ThreadLocal<SimpleDateFormat> threadlocal = new ThreadLocal<SimpleDateFormat>();
	private static final Object object = new Object();
	
	/**
	 * ��ȡSimpleDateFormat
	 * @param pattern
	 * @return SimpleDateFormat ����
	 * @throws RuntimeException
	 */
	private static SimpleDateFormat getDateFormat (String pattern) throws RuntimeException{
		SimpleDateFormat dateFormat = threadlocal.get();
		if(dateFormat == null){
			synchronized (object) {
				if(dateFormat == null){
					dateFormat = new SimpleDateFormat(pattern);
					dateFormat.setLenient(false);
					threadlocal.set(dateFormat);
				}
			}
		}
		dateFormat.applyPattern(pattern);
		return dateFormat;
	}
	/**
	 * ��ȡ�����е�ĳ����ֵ�������·�
	 * @param date
	 * @param dateType
	 * @return
	 */
	private static int getInteger(Date date, int dateType) {
		int num = 0;
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
			num = calendar.get(dateType);
		}
		return num;
	}
	/**
	 * �ж��ַ����Ƿ�ʱ�����ַ���
	 * @param date
	 * @return
	 */
	public static boolean isDate( String date) {
		boolean isDate = false;
//		if(date != null){
//			if (getDateStyle(date) != null) {
//				isDate = true;
//			}
//		}
		return isDate;
	}
	/**
	 * ������ת��Ϊ�����ַ�����ʧ�ܷ���null
	 * @param date ����
	 * @param pattern ���ڸ�ʽ
	 * @return �����ַ���
	 */
	public static String datetoString(Date date, String pattern) {
		String dateString = null;
		if (date != null) {
			try {
				dateString = getDateFormat(pattern).format(date);
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
		}
		return dateString;
	}
	
	public static String getDate(Date date) {
		return datetoString(date, "yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * ��ȡ��ǰ�����
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		return getInteger(date, Calendar.YEAR);
	}
	/**
	 * ��ȡ��ǰ�����
	 * @param date
	 * @return
	 */
	public static int getCurrentYear(){
		Date date = new Date();
		return getInteger(date, Calendar.YEAR);
	}
	/**
	 * ��ȡ��ǰ���·�
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		return getInteger(date, Calendar.MONTH) + 1;
	}
	/**
	 * ��ȡ��ǰ���·�
	 * @param date
	 * @return
	 */
	public static int getCurrentMonth() {
		Date date = new Date();
		return getInteger(date, Calendar.MONTH) + 1;
	}
	
	public static int getDay(Date date) {
		return getInteger(date, Calendar.DATE);
	}
	
	public static int getHour(Date date) {
		return getInteger(date, Calendar.HOUR_OF_DAY);
	}
	
	public static int getMinute( Date date) {
		return getInteger(date, Calendar.MINUTE);
	}
	
	public static int getSecond(Date date) {
		return getInteger(date, Calendar.SECOND);
	}
	/**
	 * ֱ�ӵõ���ǰ����
	 * @return
	 */
	public static String getCurrentDate() {
		Date date = new Date();
		String currentDate = getDate(date);
		return currentDate;
	}
	
	public static Date stringToDate( String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = null;
		try {
			date2 = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date2;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Date date = new Date();
		String d = getCurrentDate();
		Date std = stringToDate(d);
		String da = getDate(date);
		Integer year =getYear(date);
		Integer month =  getMonth(date);
		System.out.println("date = " + std+"data 0"+date +"  = "+ year +month);
		System.out.println("da = "+stringToDate(da));
//		http://blog.csdn.net/wangpeng047/article/details/8270466
//		http://blog.csdn.net/wangpeng047/article/details/8243081
	}

}
